﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using mvc3App.DataStore.Entities;
using System.Data.Services.Client;

namespace mvc3App.DataStore.TableStorage
{
    public class TableStorage
    {
        internal static string occasionTable = "OccasionTable"; 
        internal static string onedrinkTable = "OneDrinkTable";
        internal static string beverageTable = "BeverageTable";
     
        public CloudStorageAccount storageAccount;
        public CloudTableClient tableClient;
        public TableServiceContext serviceContext;

        public TableStorage()
        {
            storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
            tableClient = storageAccount.CreateCloudTableClient();
            serviceContext = tableClient.GetDataServiceContext();            
        }



        public void InsertEntities(Occasion occasion)
        {
            tableClient.CreateTableIfNotExist(occasionTable);
            tableClient.CreateTableIfNotExist(onedrinkTable);
            tableClient.CreateTableIfNotExist(beverageTable);
            OccasionEntity occasionEntity = new OccasionEntity(occasion);
            occasionEntity.Insert(occasionTable, serviceContext);
            foreach (OneDrink item in occasion.DrinkList)
            {
                new OneDrinkEntity(occasion.OccasionID, item).Insert(onedrinkTable, serviceContext);
                try
                {
                    new BeverageEntity(item).Insert(beverageTable, serviceContext);
                }
                catch (DataServiceRequestException e)
                { }
            }
        }

        public Occasion GetEntity(Guid occasionID)
        {
            OccasionEntity occasionEntity = (from e in serviceContext.CreateQuery<OccasionEntity>(occasionTable)
                                             where e.RowKey == occasionID.ToString()
                                             select e).FirstOrDefault();
            CloudTableQuery<OneDrinkEntity> onedrinkEntityQuery = (from e in serviceContext.CreateQuery<OneDrinkEntity>(onedrinkTable)
                                                                   where e.PartitionKey == occasionID.ToString()
                                                                   select e).AsTableServiceQuery<OneDrinkEntity>();
            List<OneDrink> drinkList = new List<OneDrink>();
            BeverageEntity beverageEntity = new BeverageEntity();
            foreach (OneDrinkEntity item in onedrinkEntityQuery)
            {
                beverageEntity = (from e in serviceContext.CreateQuery<BeverageEntity>(beverageTable)
                                  where e.RowKey == item.RowKey
                                  select e).FirstOrDefault();
                drinkList.Add(new OneDrink(item, beverageEntity));
            }
            Occasion occasion = new Occasion(occasionEntity, drinkList);
            return occasion;
        }

        public List<Occasion> ListEntities(Guid userId)
        {
            CloudTableQuery<OccasionEntity> occasionEntityQuery = (from e in serviceContext.CreateQuery<OccasionEntity>(occasionTable)
                                                                   where e.PartitionKey == userId.ToString()
                                                                   select e).AsTableServiceQuery<OccasionEntity>();
            List<Occasion> occasionList = new List<Occasion>();
            foreach (OccasionEntity item in occasionEntityQuery)
            {
                occasionList.Add(GetEntity(item.OccasionId));
            }
            return occasionList;
        }

        public List<Beverage> ListBeverages()
        {
            CloudTableQuery<BeverageEntity> beverageEntityQuery = (from e in serviceContext.CreateQuery<BeverageEntity>(beverageTable)
                                                                   where e.PartitionKey == ""
                                                                   select e).AsTableServiceQuery<BeverageEntity>();
            List<Beverage> beverageList = new List<Beverage>();
            foreach (BeverageEntity item in beverageEntityQuery)
            {
                beverageList.Add(new Beverage(item.Name, item.AlcoholPercent));
            }
            return beverageList;
        }
    }
}